package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Classes;
import com.market.service.ClassesService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Resource
    ClassesService classesService;

    /**
     * 搜索功能
     *
     * @param namesOrId
     * @return
     * @throws ControllerException
     */
    @GetMapping("/search")
    public ResponseResult<List<Classes>> search(@RequestParam String namesOrId) throws ControllerException {
        List<Classes> classes = classesService.searchClasses(namesOrId);
        return ResponseResult.success(ResponseCode.SUCCESS, classes);

    }

    /**
     * 添加类别
     * @param name
     * @return
     * @throws ControllerException
     */
    @GetMapping ("/add")
    public ResponseResult<Integer> addClasses(@RequestParam String name)throws ControllerException{
        int instre= classesService.add(name);
        return ResponseResult.success(ResponseCode.SUCCESS,instre);
    }

    /**
     * 添加
     * @param classes
     * @param name
     * @return
     * @throws ControllerException
     */
    @GetMapping("/addSub")
    public ResponseResult<Integer> add(@RequestParam Classes classes,@RequestParam String name)throws ControllerException{
        Classes cla = new Classes();
        cla.setCommodityClassesName(name);
        int addSubName = classesService.addSubName(classes, name);
        return ResponseResult.success(ResponseCode.SUCCESS,addSubName);
    }

    /**
     * 修改
     * @param classesId
     * @param classesName
     * @return
     * @throws ControllerException
     */
    @GetMapping("/chagerSub")
    public ResponseResult<Integer> chagerClasses(@RequestParam Integer classesId,@RequestParam String classesName) throws ControllerException{
        int chagerClasses = classesService.chagerSub(classesId, classesName);
        return ResponseResult.success(ResponseCode.SUCCESS,chagerClasses);
    }

    /**
     * 删除
     * @param classesId
     * @return
     * @throws ControllerException
     */
    @GetMapping("/deleteSub")
    public ResponseResult<Integer> deleteSub(@RequestParam Integer classesId)throws ControllerException{
        int deleteClasses = classesService.deleteClasses(classesId);
        return ResponseResult.success(ResponseCode.SUCCESS,deleteClasses);
    }
}
